home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CUJ9209.ARJ / 1009027A < prev    next >
Text File  |  1992-07-21  |  3KB  |  135 lines

  1. STRING    \"[^"]*
  2. CHARC    \'[^']*
  3. AB    "//"
  4. CB    "/*"
  5. CE    "*/"
  6. ESC    "\\"
  7. BB    "{"
  8. BE    "}"
  9. SP    [ \n\t]
  10. STMNT    .*";"
  11. TRY    "try"{SP}*{BB}
  12. CATCH    "catch"{SP}*"("{SP}*/.*")"{SP}*"{"
  13. ELIPS    "..."{SP}*")"{SP}*"{"
  14. ARGU    .*{SP}*")"{SP}*"{"
  15. CEMPTY    {SP}*")"{SP}*"{"
  16. THROW    "throw".*";"
  17. THROWD    ")"{SP}*"throw"{SP}*"(".*")"/{SP}*"{"
  18. %START Try Catch CatchArg ThrowD End
  19. %{
  20. #include <string.h>
  21. int scope;
  22. char *throwArgs[YYLMAX];
  23. %}
  24. %%
  25. {STRING}|{CHARC}    {    /* Skip character or string constants */
  26.                 if(yytext[yyleng-1] == '\\')
  27.                     yymore();
  28.                 else
  29.                     printf("%s%c", yytext, input());
  30.             }
  31. {AB}.*$    { /* C++ comment handling */ ECHO; }
  32. {CB}    { /* C comment handling */
  33.             int c;
  34.             ECHO;
  35.             while(1) {
  36.                 output(c=input());
  37.                 /* Check for end of comment */
  38.                 if( c == '*' ) {
  39.                     if((c=input()) == '/') {
  40.                         output(c);
  41.                         break;
  42.                     }
  43.                     unput(c);
  44.                 }
  45.             }
  46.         }
  47. {TRY}    {
  48.             printf("if(setjmp(*ExH::stk++)==0) {");
  49.             scope=0;
  50.             BEGIN Try;
  51.         }
  52. <Try>{BE}        { 
  53.             ECHO; 
  54.             if(--scope < 0) {
  55.                  printf("ExH::stk--; }"); /* Pop */
  56.                  BEGIN Catch;
  57.             }
  58. <Catch,End>{CATCH}    { scope = 0; BEGIN CatchArg; }
  59. <CatchArg>{ELIPS}    {
  60.             printf("else {");
  61.             BEGIN 0;
  62.         }
  63. <CatchArg>{CEMPTY}    {
  64.             fprintf(stderr, "ERROR: Catch must have an argument\n");
  65.             exit(1);
  66.         }
  67. <CatchArg>{ARGU}    {
  68.             char *type, *ref, *var=0;
  69.  
  70.             /* Looks for reference */
  71.             type = strtok(yytext, ")");
  72.             ref = strchr(type, '&');
  73.  
  74.             /* Identifies the type */
  75.             type = strtok(yytext, " \t\n&");
  76.             printf("else if( ExH::ex()->type() == %s::stype()) {", type);
  77.  
  78.             /* Looks for a variable declaration */
  79.             if(var = strtok(NULL, " \t\n)")) {
  80.                 if(ref) ref = "&";
  81.                 else ref = "";
  82.                 printf(" %s%s %s = *((%s*)ExH::ex());", type, ref, var, type);
  83.             }
  84.             BEGIN Catch;
  85.         }
  86. <Catch,Try,ThrowD>{BB}    {ECHO; ++scope;}
  87. <Catch>{BE}    { 
  88.             ECHO; 
  89.             if(--scope < 0) BEGIN End;
  90.         }
  91. <End>{STMNT}    |
  92. <End>{BE}    {
  93.             printf("else unexpected();");
  94.             ECHO;
  95.             BEGIN 0;
  96.         }
  97. {THROW}    {    /* Normal throw */
  98.             char *p;
  99.  
  100.             /* skip 'throw' and pass the rest of the sentence as argument */
  101.             p = strtok(yytext+5, ";");
  102.             if(!p) p = "";
  103.             printf("ExH::throw(%s);", p);
  104.         }
  105. {THROWD}    {    /* Throw Declaration ( type f() throw (x1, x2, etc) ) */
  106.             char *p;
  107.             p = strstr(yytext, "throw");
  108.             *p = 0;
  109.  
  110.             /* Save throw args for future catchs */
  111.             strcpy(throwArgs, p+5);
  112.  
  113.             /* Prints function name and start with try */
  114.             printf("%s{ if(setjmp(*ExH::try_())==0)", yytext);
  115.  
  116.             /* Prints the function body */
  117.             scope = 0;
  118.             BEGIN ThrowD;
  119.         }
  120. <ThrowD>{BE}    {
  121.             ECHO;
  122.             if(--scope <= 0) {
  123.                 char *p;
  124.  
  125.                 p = strchr(throwArgs, '(') + 1;
  126.                 while(p=strtok(p, " ,)")) {
  127.                     printf("else if( ExH::ex()->type() == %s::stype()) ", p);
  128.                     printf("ExH::throw(*ExH::ex());");
  129.                     p = 0;
  130.                 }
  131.                 printf("else unexpected(); }");
  132.                 BEGIN 0;
  133.             }
  134.         }
  135.